﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using DataAccess;
using System.Threading;
namespace Controller
{
    /// <summary>
    /// check connection controller handles connection with selected server.
    /// </summary>
    public class CheckConnectionController
    {
        public event CheckConnectionEventHandler CheckConnectionCompleted;

        private System.ComponentModel.BackgroundWorker backgroundWorker = new System.ComponentModel.BackgroundWorker();

        public CheckConnectionController()
        {
            backgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(backgroundWorker_DoWork);
        }
        
        public void CheckConnectionAsyncronus(ConnectionInformation CI)
        {
            backgroundWorker.RunWorkerAsync(CI);
        }

        void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            CheckConnection((ConnectionInformation)e.Argument);
        }

        public void OnCheckConnectionCompleted(CheckConnectionEventArgs e)
        {
            if (CheckConnectionCompleted != null)
            {
                CheckConnectionCompleted(e);
            }

        }

        public bool IfAccessFileHavingPassword(ConnectionInformation CI)
        {
            if (CI.DataBaseType == DataBaseType.MSAccess)
            {
                MSAccessDataAccess mSAccessDataAccess = new MSAccessDataAccess();
                CI.Password = "";
                CI.AuthenticationMode = AuthenticationMode.MSAccessWithoutPassword;
                return !mSAccessDataAccess.IsAccessFileHavingPassword(CI);
            }
            else{
                throw new Exception("Database type is not selected as access file.");
            } 
        }

        public void CheckConnection(ConnectionInformation CI)
        {
            
            List<string> MSSQLresult = new List<string>();
            bool MSAccessResult;

            try
            {

                if (CI.DataBaseType == DataBaseType.MSSQLServer)
                {
                    MSSQLDataAccess mSSQLDataAccess = new MSSQLDataAccess();
                    MSSQLresult = mSSQLDataAccess.CheckConnection(CI);
                    OnCheckConnectionCompleted(new CheckConnectionEventArgs(MSSQLresult));
                }
                else if (CI.DataBaseType == DataBaseType.MSAccess)
                {
                    MSAccessDataAccess mSAccessDataAccess = new MSAccessDataAccess();
                    MSAccessResult = mSAccessDataAccess.CheckConnection(CI);
                    OnCheckConnectionCompleted(new CheckConnectionEventArgs() { ConectedWithAccessFile=MSAccessResult});
                }
                else if (CI.DataBaseType == DataBaseType.MySQL)
                {
                    MySQLDataAccess mySQLDataAccess = new MySQLDataAccess();
                    MSSQLresult = mySQLDataAccess.CheckConnection(CI);
                    OnCheckConnectionCompleted(new CheckConnectionEventArgs(MSSQLresult));
                }
            }
            catch (Exception ex)
            {
                OnCheckConnectionCompleted(new CheckConnectionEventArgs(ex));
            }
        }
    }

}
